package utils;

import java.sql.*;

public class DBUtil {
    private static final String USER = "root";
    private static final String PWD = "1342701476";
    private static final String URL = "jdbc:mysql://localhost:3306/employees?autoReconnect=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true";
    private static Statement stmt;
    private static Connection con;

    private static ResultSet rs = null;

    private static DBUtil utils = null;
    private static PreparedStatement pstmt; // 预编译语句对象

    private DBUtil() {

    }

    // 不是线程安全的 如果有并发访问实例化的时候会出现线程安全的问题，解决办法加同步锁synchronized
    public synchronized static DBUtil getDBUtil() {
        if (utils == null) {
            utils = new DBUtil();
            return utils;
        }
        return utils;
    }

    public static Connection getConn() {
        if (con == null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                con = DriverManager.getConnection(URL, USER, PWD);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return con;
    }



    public static Statement getStatement() {
        if (stmt == null) {
            try {
                if (con == null) {
                    con = getConn();
                }
                stmt = con.createStatement();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return stmt;
    }

    public static void closeMySQL() {
        try {
            if (rs != null)
                rs.close();
            rs = null;
            if (stmt != null)
                stmt.close();
            stmt = null;
            if (con != null)
                con.close();
            con = null;
        } catch (Exception e) {
            e.printStackTrace();// 输出当前异常对象的堆栈使用轨迹
        }
    }
}